<script lang="ts" setup name="XtxCity">
import { onClickOutside } from '@vueuse/core';
import { AreaList, CityResult } from '@/types/goods'
import { ref, watch } from 'vue';
import axios from 'axios'

// 子组件进行接收-用户的地址
defineProps<{
  userAddress?: string
}>()


// 控制城市选择-弹层的显示隐藏
const active = ref(false)
const target = ref(null)
// 城市列表 - 初始数据
const cityList = ref<AreaList[]>([])
const cacheList = ref<AreaList[]>([])
// 点击控制
const toggle = () => {
  active.value = !active.value
}
// 保存选择的城市信息
const changeResult = ref({
  provinceName: '', // 省份名字
  provinceCode: '', // 省份编码
  cityName: '',     // 市名字
  cityCode: '',    // 市编码
  countyName: '',   // 区名字
  countyCode: '',   // 区编码
})

const emit = defineEmits<{
  (e: 'changeCity', value: CityResult): void
}>()


// 侦听元素外部的点击
onClickOutside(target, () => {
  // 对应的弹层-隐藏
  active.value = false
})

// 获取城市数据
const getCityList = async () => {
  const res = await axios.get<AreaList[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  // console.log('城市的列表数据', res);
  cityList.value = res.data
  cacheList.value = res.data
}
getCityList()

// 城市选择 - 注册点击事件
const selectCity = (city: AreaList) => {
  console.log(city);
  if (city.level === 0) {
    // 省 level === 0
    // city.name = 城市名字
    // city.code = 城市编码
    changeResult.value.provinceName = city.name
    changeResult.value.provinceCode = city.code
    cityList.value = city.areaList
  }
  if (city.level === 1) {
    // 市 level === 1
    changeResult.value.cityName = city.name
    changeResult.value.cityCode = city.code
    cityList.value = city.areaList
  }
  if (city.level === 2) {
    // 区 level === 2
    changeResult.value.countyName = city.name
    changeResult.value.countyCode = city.code
    // 关闭弹窗
    active.value = false
    console.log('选中的城市信息', changeResult.value);
    // 派发事件-子传父
    emit('changeCity', changeResult.value)
  }

}

// 监听弹窗关闭, 只要弹窗关闭, 需要恢复成原始数据 - cacheList
// 注意点: 这里是不需要使用watchEffect, 不需要一进入页面就执行一次
// watchEffect 使用场景, 一般都是处理路由缓存      
watch(active, (newValue) => {
  // 弹窗关掉
  if (!newValue) {
    // 恢复数据
    cityList.value = cacheList.value
  }
})

</script>
<template>
  <div class="xtx-city" ref="target">
    <!-- 城市选择-点击控制 -->
    <div class="select" :class="{ active: active }" @click="toggle">
      <span class="value" v-if="userAddress">{{ userAddress }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 城市选择-弹层控制 -->
    <div class="option" v-show="active">
      <span class="ellipsis" @click="selectCity(item)" v-for="item in cityList" :key="item.code">
        {{ item.name }}
      </span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 5px;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    >span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>